package mi.payment.controller;

import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.List;

import mi.payment.R;
import mi.payment.ui.print.helper.SumTotal;
import mi.payment.ui.print.helper.TotalSettlementDetail;
import mi.payment.constants.TransTypeSettlement;
import mi.payment.ui.base.ParentActivity;

/**
 * 结算总计单界面统计的view控制器
 *
 * @author Created by Lee64 on 2017/12/13.
 */

public class BaseTotalSettlementDetailController extends BaseController {

    public BaseTotalSettlementDetailController(ParentActivity activity) {
        super(activity);
    }

    public View getSettlementDetailView(TotalSettlementDetail detail) {
        View view = LayoutInflater.from(activity).inflate(R.layout.layout_base_total_settlement_detail_controller, null);
        TextView inCard = (TextView) view.findViewById(R.id.in_card);
        TextView bankCard = (TextView) view.findViewById(R.id.bank_card);
        TextView scanCode = (TextView) view.findViewById(R.id.scan_payment);
        TextView foreignCard = (TextView) view.findViewById(R.id.foreign_card);

        // 内卡
        String text = getString(R.string.in_card) + ":" + getString(R.string.reconciliation);
        inCard.setText(text);
        // 银行卡
        text = getString(R.string.platform_bank_card) + ":";
        bankCard.setText(text);
        // 扫码
        text = getString(R.string.scan_code) + getString(R.string.payment) + ":";
        scanCode.setText(text);
        // 外卡
        text = getString(R.string.foreign_card) + ":";
        foreignCard.setText(text);

        LinearLayout inCardTotalItem = (LinearLayout) view.findViewById(R.id.item_in_card_total);
        LinearLayout foreignCardTotalItem = (LinearLayout) view.findViewById(R.id.item_foreign_card_total);
        LinearLayout inCardBankCardItem = (LinearLayout) view.findViewById(R.id.item_in_card);
        LinearLayout foreignCardItem = (LinearLayout) view.findViewById(R.id.item_foreign_card);
        LinearLayout inCardScanPaymentItem = (LinearLayout) view.findViewById(R.id.item_scan_payment);

        if (detail == null) {
            return view;
        }

        // 银行卡
        List<SumTotal> sumTotalsNotForeign = detail.sumTotalsNotForeign;
        if (sumTotalsNotForeign != null && sumTotalsNotForeign.size() > 0) {
            for (SumTotal s : sumTotalsNotForeign) {
                String name = s.getTransType().trim();
                String money = s.getSumMoney();
                String transType = TransTypeSettlement.getName(name);
                String transTimes = s.getTransTimes();
                String transMoney = transMoneyValue(money);
                View item = createItem(transType, transTimes, transMoney);
                inCardBankCardItem.addView(item);
            }
        }

        // 扫码
        List<SumTotal> sumTotalsQRCode = detail.sumTotalsQRCode;
        if (sumTotalsQRCode != null && sumTotalsQRCode.size() > 0) {
            for (SumTotal s : sumTotalsQRCode) {
                if (TextUtils.equals("0", s.getTransTimes())) {
                    continue;
                }
                String name = s.getTransType().trim();
                String money = s.getSumMoney();
                String transType = TransTypeSettlement.getName(name);
                String transTimes = s.getTransTimes();
                String transMoney = transMoneyValue(money);
                View item = createItem(transType, transTimes, transMoney);
                inCardScanPaymentItem.addView(item);
            }
        }

        // 内卡总计
        String itemName = getString(R.string.in_card) + getString(R.string.summny);
        String itemNum = detail.totalNum;
        String itemMoney = transMoneyValue(detail.totalMoney);
        fillTotalData(inCardTotalItem, itemName, itemNum, itemMoney);

        // 外卡数据
        List<SumTotal> sumTotalsForeign = detail.sumTotalsForeign;
        if (sumTotalsForeign != null && sumTotalsForeign.size() > 0) {
            for (SumTotal s : sumTotalsForeign) {
                String name = s.getTransType().trim();
                String money = s.getSumMoney();
                String transType = TransTypeSettlement.getName(name);
                String transTimes = s.getTransTimes();
                String transMoney = transMoneyValue(money);
                View item = createItem(transType, transTimes, transMoney);
                foreignCardItem.addView(item);
            }
        }

        // 外卡总计
        itemName = getString(R.string.foreign_card) + getString(R.string.summny);
        itemNum = detail.totalNumForeign;
        itemMoney = transMoneyValue(detail.totalMoneyForeign);
        fillTotalData(foreignCardTotalItem, itemName, itemNum, itemMoney);

        return view;
    }

    protected void addTotalItemData(LinearLayout view, List<SumTotal> list) {
        if (list == null || list.size() == 0) return;
        for (SumTotal sumTotal : list) {
            String name = sumTotal.getTransType().trim();
            String money = sumTotal.getSumMoney();
            String transType = TransTypeSettlement.getName(name);
            String transTimes = sumTotal.getTransTimes();
            String transMoney = transMoneyValue(money);
            View item = createItem(transType, transTimes, transMoney);
            view.addView(item);
        }
    }

    protected View createItem(String transType, String transTimes, String transMoney) {
        View layout = LayoutInflater.from(activity).inflate(R.layout.item_total_settlement, null);
        TextView name = (TextView) layout.findViewById(R.id.tv_name);
        TextView sum = (TextView) layout.findViewById(R.id.tv_sum);
        TextView money = (TextView) layout.findViewById(R.id.tv_money);
        name.setText(transType);
        sum.setText(transTimes);
        money.setText(transMoney);
        return layout;
    }

    protected void fillTotalData(View layout, String label, String transTimes, String transMoney) {
        TextView name = (TextView) layout.findViewById(R.id.tv_name);
        TextView sum = (TextView) layout.findViewById(R.id.tv_sum);
        TextView money = (TextView) layout.findViewById(R.id.tv_money);
        name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        name.setText(label);
        sum.setText(transTimes);
        money.setText(transMoney);
    }

    protected String transMoneyValue(String str) {
        return new BigDecimal(str).movePointLeft(2).toPlainString();
    }

}
